﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Waaahsabi.Controls.DWM
{
	public class StartMenu : Control
	{
		private ItemsControl _itmProgramGroups;
		private Button _btnLogoff;
		private ItemsControl _itmClientFeatures;


		public static readonly DependencyProperty GroupsProperty =
		DependencyProperty.Register
			(
			"Groups",
			typeof(ObservableCollection<StartMenuProgramGroup>),
			typeof(StartMenu),
			new PropertyMetadata(null)
			);

		public ObservableCollection<StartMenuProgramGroup> Groups
		{
			get { return (ObservableCollection<StartMenuProgramGroup>)GetValue(GroupsProperty); }
			set
			{
				SetValue(GroupsProperty, value);
				value.CollectionChanged += GroupsCollectionChanged;
			}
		}

		public static readonly DependencyProperty ClientFeaturesProperty =
		DependencyProperty.Register
			(
			"ClientFeatures",
			typeof(ObservableCollection<StartMenuProgramGroupItem>),
			typeof(StartMenu),
			new PropertyMetadata(null)
			);

		public ObservableCollection<StartMenuProgramGroupItem> ClientFeatures
		{
			get { return (ObservableCollection<StartMenuProgramGroupItem>)GetValue(ClientFeaturesProperty); }
			set { SetValue(ClientFeaturesProperty, value); }
		}

		#region ClientIP DependencyProperty
		public static readonly DependencyProperty ClientIPProperty =
		DependencyProperty.Register
			(
			"ClientIP",
			typeof(string),
			typeof(StartMenu),
			new PropertyMetadata(null)
			);

		public string ClientIP
		{
			get { return (string)GetValue(ClientIPProperty); }
			set { SetValue(ClientIPProperty, value); }
		}
		#endregion


		public event EventHandler CloseButtonClicked;
		public event EventHandler<ProgramItemExecuteArgs> ProgramExecuteRequested;

		public StartMenu()
		{
			DefaultStyleKey = typeof(StartMenu);
			Groups = new ObservableCollection<StartMenuProgramGroup>();
			Groups.CollectionChanged += GroupsCollectionChanged;
			ClientFeatures = new ObservableCollection<StartMenuProgramGroupItem>();
			ClientFeatures.CollectionChanged += ClientFeaturesCollectionChanged;
		}

		void ClientFeaturesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if(e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach (StartMenuProgramGroupItem item in e.NewItems)
					item.Click += ItemClick;
			}
		}

		void GroupsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if(e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach (StartMenuProgramGroup group in e.NewItems)
				{
					group.GroupOpened += GroupGroupOpened;
				}
			}
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_itmProgramGroups = GetTemplateChild("itmProgramGroups") as ItemsControl;
			if(_itmProgramGroups != null)
			{
				_itmProgramGroups.ItemsSource = Groups;
			}
			_btnLogoff = GetTemplateChild("btnLogoff") as Button;
			if(_btnLogoff != null)
				_btnLogoff.Click += (s, e) =>
				{
					if (CloseButtonClicked != null)
						CloseButtonClicked(this, null);
				};
			_itmClientFeatures = GetTemplateChild("itmClientFeatures") as ItemsControl;
			if (_itmClientFeatures != null)
			{
				_itmClientFeatures.Items.Clear();
				_itmClientFeatures.ItemsSource = ClientFeatures;
			}
		}

		public void AddProgramGroup(ulong groupId, string groupName)
		{
			StartMenuProgramGroup group = new StartMenuProgramGroup();
			group.CategoryId = groupId;
			group.CategoryName = groupName;
			group.GroupClosed += group_GroupClosed;
			group.GroupOpened += GroupGroupOpened;
			Groups.Add(group);
		}

		public void RemoveProgramGroup(ulong groupId)
		{
			StartMenuProgramGroup grp = (from g in Groups where g.CategoryId == groupId select g).FirstOrDefault();
			if (grp == null)
				return;
			Groups.Remove(grp);
		}

		void GroupGroupOpened(object sender, EventArgs e)
		{
			StartMenuProgramGroup openedGroup = sender as StartMenuProgramGroup;
			if(openedGroup == null)
				return;
			foreach(StartMenuProgramGroup group in Groups)
			{
				if(group == openedGroup) 
					group.Open();
				else
					group.Close();
			}
		}

		void group_GroupClosed(object sender, EventArgs e)
		{
		}

		public void AddProgramToGroup(ulong groupId, ulong programId, string programName)
		{
			StartMenuProgramGroup group = Groups.Where(it => it.CategoryId == groupId).FirstOrDefault();
			if(group == null) return;
			StartMenuProgramGroupItem item = new StartMenuProgramGroupItem();
			item.InterfaceId = programId;
			item.ProgramName = programName;
			item.Click += ItemClick;
		}

		void ItemClick(object sender, ProgramItemExecuteArgs e)
		{
			if (ProgramExecuteRequested != null)
				ProgramExecuteRequested(sender, e);
		}
	}
}
